package com.example.userservice.controller;

import com.example.userservice.dto.LoginDTO;
import com.example.userservice.dto.RegisterDTO;
import com.example.userservice.service.UserService;
import com.example.userservice.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    public UserService userService;


    @PostMapping("/register")
    public ResponseEntity<String> register(@RequestBody RegisterDTO register){
        userService.register(register);
        return ResponseEntity.ok(register.getUsername()+"注册成功");
    }
    @PostMapping("/login")
    public String login(@RequestBody LoginDTO login){
        return userService.login(login);
    }
    @GetMapping("/users")
    public List<String> getUsers(@RequestParam Long userId){
        return new ArrayList<>(userService.getUsers(userId));
        //return ResponseEntity.ok("获取用户列表成功");
    }

    @GetMapping("/{userId}")
    public List<User> select(@PathVariable Long userId){
        return new ArrayList<>(userService.select(userId));
    }

    @PutMapping("/{userId}")
    public ResponseEntity<String> change(@PathVariable Long userId,@RequestBody User user){
        int flag = userService.change(userId,user);
        if (flag == 0){
            return ResponseEntity.ok("权限不足");
        }else{
            return ResponseEntity.ok("修改成功");
        }
    }
    @PostMapping("/reset-password")
    public ResponseEntity<String> resetPassword(@RequestParam Long userId){
        userService.resetPassword(userId);
        return ResponseEntity.ok("重置密码成功");
    }
    @PostMapping("/grade")
    public ResponseEntity<String> grade(@RequestParam Long userId,@RequestParam Long targetId){
        int flag = userService.grade(userId,targetId);
        if (flag == 0){
            return ResponseEntity.ok("权限不足");
        }else{
            return ResponseEntity.ok("操作成功");
        }
    }
}
